<?
if (!isset($r)) $r = "../";
require_once($r."_engine/functions.php");
require_once($r."_engine/header_nocache.php");
require_once($r."_engine/c_feed.php");
require_once($r."_engine/c_sql_table.php");
require_once($r."_engine/c_monitor.php");
require_once($r."_engine/c_account.php");
require_once($r."_engine/c_sp_gate.php");



//-----------------------------------------------------------------------------------------------------------------------------------

class API_method {
	public $params = false;

	public function __construct($aAction=false, $aAPIkey=false){
	
		
	
		$this->action = $aAction;
		$this->APIkey = $aAPIkey;
		if ($_POST) $this->params = $_POST;
		else $this->params = $_GET;
		
		// TEMP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		$this->APIkey="x";
		
		if (!$this->params) exit("Parameters are required!");
		if (!$this->APIkey) exit("W3M v1 API key is required!");
		if (!$this->action) exit("Action parameter is required!");
		if (!$this->_validateAPIkey()) exit("API key is incorrect!");
	}
	
	//-------------------------------------------------------------------------------------------------------------------------
	
	private function _validateAPIkey($APIkey=false){
		if (!$APIkey) $APIkey = $this->APIkey;
		return true;
	}
	
	//-------------------------------------------------------------------------------------------------------------------------
	
	public function UserCheckLogin ($login,$pass) {
		if ($this->action!="login") exit("Incorrect Action");
		
		define("LOGIN_INCORRECT", 1);
		define("LOGIN_NOT_ACTIVATED", 2);
		define("LOGIN_ACCOUNT_EXIPIRED", 3);
		$account = new CAccount();
		if (!$account->LoadByEmail($login) || 
			$pass != $account->data["password"]) 
		{
			$status = LOGIN_INCORRECT;
			echo "Login Failure";
		}
		else 
		{
			if (!$account->data["activated"])
			{
				$status = LOGIN_NOT_ACTIVATED;
			}
			else
			{
				if ($account->data["expire"] < gmt_time())
				{
					$status = LOGIN_ACCOUNT_EXIPIRED;
				}
				else 
				{
					$account->Update(array(
						"last_login"	=> $account->data["current_login"],
						"current_login"	=> gmt_time(),
					));

					//$g_session->Set($account->id, $account->data["password"], isset($f_remember));
					
					echo "Login Succeeded";
					//generate_html_redirect(@$ref ? $ref : "dashboard.php");
				}
			}
		}
	}
	
	//-------------------------------------------------------------------------------------------------------------------------
	
	public function UserSignUp ($f_first_name,$f_email,$f_password) {
		if ($this->action!="signup") exit("Incorrect Action");

		$a = new CAccount();
		$res = $a->CreateNew();
		
		echo $res;
		
		if ($res==2) echo "Email already exists";
		else if ($res==1) echo "SigUp Succeeded";
		else echo "SignUp Failed";
		
		$a->LoadByEmail($f_email);
		$a->Activate( $a->data["activation_code"] );

		$a->Update(array(
			"last_login"	=> gmt_time(),
			"current_login"	=> gmt_time(),
		));
		return $a->id;
	}
	
	//-------------------------------------------------------------------------------------------------------------------------
	
	public function GetMonitorRSS ($monitor_id) {
		if ($this->action!="monitor_rss") exit("Incorrect Action");
		
		/* Load monitor - first of all */
		$m = new CMonitor();
		$r = $m->LoadByQuery($q =
			"SELECT * FROM monitors ".
			//"WHERE id='".$mid."' AND activation_code='$guid';"
			"WHERE id='".$monitor_id."';"
		);

		if (!$r) {
			/* monitor not found or wrong guid => page not found */
			//@header("HTTP/1.0 404 Not Found");
			//die();
			exit("Monitor not found!");
		}

		$feeds = execute_query($q = 
			"SELECT * FROM feeds ".
			"WHERE monitor_id='".$monitor_id."' ".
			"LIMIT 1"
		);
		if (@mysql_num_rows($feeds) > 0)
		{
			$feed = @mysql_fetch_array($feeds);
			@mysql_free_result($feeds);
			echo "\n";
			print_feed($feed["feed"]);
		} else exit("No feed generated for this monitor!");

	}
	
	//-------------------------------------------------------------------------------------------------------------------------
	
	public function CreateMonitor () {
		if ($this->action!="monitor_create") exit("Incorrect Action");
		
		$a = new CAccount();
		$a->LoadById(69);
		$mid = 0;
		
		$valArr = array();
		$valArr['url'] = urldecode($this->params['url']);
		$valArr['title'] = urldecode($this->params['url']);
		if ($this->params['frequency']) $valArr['frequency'] = $this->params['frequency'];
		if ($this->params['emails']) $valArr['alert_emails'] = $this->params['emails'];
		
		$monitor = new CMonitor();
		$mid = $monitor->CreateNew($a, $valArr);
		if ( $mid>0 ) echo $mid;
		else echo "Monitor Creation Failed!";

	}
	
	//-------------------------------------------------------------------------------------------------------------------------
	
	public function DeleteMonitor ($monitor_id) {
		if ($this->action!="monitor_delete") exit("Incorrect Action");
		
		global $g_st_monitor;
		if ( $g_st_monitor->Delete(array($monitor_id)) ) echo "Monitor deleted!";
		else echo "Monitor not deleted!";

	}
	
	//-------------------------------------------------------------------------------------------------------------------------
	

}



/*
SAMPLE:
Place w3m_connector.php in the website root and insert code below anywhere you need

//------------------------------------------------------------------------------
require_once("/w3m_connector.php");

$api = new API_action("signup");	
$api->UserSignUp("UserName","oggg@cid.kiev.ua","11111");

$api = new API_action("login");	
$api->UserCheckLogin("og@cid.kiev.ua","11111");

$api = new API_action("monitor_rss");	
$api->GetMonitorRSS(282);

*/




//echo "<br>OK";

?>